package com.ruan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruan.dto.AddOrderDto;
import com.ruan.entity.Order;
import com.ruan.entity.R;
import com.ruan.entity.User;
import com.ruan.mapper.UserMapper;
import com.ruan.query.OrderQuery;
import com.ruan.service.OrderService;
import com.ruan.mapper.OrderMapper;
import com.ruan.utils.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

/**
* @author 59499
* @description 针对表【tb_order】的数据库操作Service实现
* @createDate 2023-09-20 15:00:05
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
    implements OrderService{

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public R<IPage<Order>> queryOrders(OrderQuery orderQuery) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.select("order_id","title","price","content","type","own_name","picture","create_time");
        if (orderQuery.getSearchValue() != null && !Objects.equals(orderQuery.getSearchValue(), "")){
            wrapper.like("title",orderQuery.getSearchValue());
        }
        Page<Order> orderPage = new Page<>(orderQuery.getPage(),orderQuery.getSize());
        IPage<Order> orderIPage = orderMapper.selectPage(orderPage,wrapper);
        return R.ok("查询成功",orderIPage);
    }

    @Override
    public R<Order> getOrderDetails(Integer orderId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.select("order_id","title","price","content","type","picture","create_time","update_time","address");
        wrapper.eq("order_id",orderId);
        Order order = orderMapper.selectOne(wrapper);
        return R.ok("查询成功",order);
    }

    @Override
    public R<List<Order>> getHotGoodsByLimit(Integer limit) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.select("order_id","title");
        wrapper.orderByDesc("create_time").last("limit "+limit);
        List<Order> orderList = orderMapper.selectList(wrapper);
        return R.ok("查询成功",orderList);
    }

    @Override
    public R<String> uploadOrderPicture(MultipartFile file) {
        //获取上传的文件名
        String fileName = file.getOriginalFilename();
        //产生新的文件名，时间戳+原先文件名后缀
        String newFileName = null;
        if (fileName != null) {
            newFileName = System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf('.'));
        }
        String path = "order/";
        String url = null;
        if (newFileName != null) {
            try{
                url = OssUtil.upload(newFileName, file.getInputStream(),path);
            }catch (Exception e){
                return R.error("上传失败");
            }
        }
        int lastIndex = 0;
        if (url != null) {
            lastIndex = url.lastIndexOf("/");
        }
        String result = url.substring(lastIndex + 1);
        return R.ok("上传成功",result);
    }

    @Override
    public R<Void> addOrder(AddOrderDto addOrderDto) {
        User user = userMapper.selectById(addOrderDto.getUserId());
        Order order = new Order();
        order.setTitle(addOrderDto.getTitle());
        order.setContent(addOrderDto.getContent());
        order.setPrice(addOrderDto.getPrice());
        order.setPicture(addOrderDto.getPicture());
        order.setOwnName(user.getNickname());
        order.setType(addOrderDto.getType());
        int row = orderMapper.insert(order);
        if (row != 1){
            return R.error("添加失败");
        }
        return R.ok("添加成功");
    }

    @Override
    public R<IPage<Order>> queryOrdersList(OrderQuery orderQuery) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.select("order_id","title","price","content","type","own_name","picture","create_time");
        wrapper.eq("type","goods");
        if (orderQuery.getSearchValue() != null && !Objects.equals(orderQuery.getSearchValue(), "")){
            wrapper.like("title",orderQuery.getSearchValue());
        }
        Page<Order> orderPage = new Page<>(orderQuery.getPage(),orderQuery.getSize());
        IPage<Order> orderIPage = orderMapper.selectPage(orderPage,wrapper);
        return R.ok("查询成功",orderIPage);
    }

    @Override
    public R<IPage<Order>> queryPurchaseList(OrderQuery orderQuery) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.select("order_id","title","price","content","type","own_name","picture","update_time");
        wrapper.eq("type","needs");
        if (orderQuery.getSearchValue() != null && !Objects.equals(orderQuery.getSearchValue(), "")){
            wrapper.like("title",orderQuery.getSearchValue());
        }
        Page<Order> orderPage = new Page<>(orderQuery.getPage(),orderQuery.getSize());
        IPage<Order> orderIPage = orderMapper.selectPage(orderPage,wrapper);
        return R.ok("查询成功",orderIPage);
    }
}




